<template>
  <div class="container">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width=""
      class="login-form"
    >
      <h2 class="title">Login</h2>
      <el-form-item prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="form.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button class="login-btn" type="primary" @click="submitForm"
          >登录</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.form.validate((valid) => {
        // console.log(valid);
        if (valid) {
          // 在此处处理表单提交逻辑，例如发送请求验证用户名密码等
          console.log(this.form);
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.container {
  width: 360px;
  margin: 200px auto;
  border: 1px solid #1890ff;
  border-radius: 8px;
  text-align: center;
}

.title {
  margin-bottom: 22px;
}

.el-form-item.is-error .el-input {
  border-color: #f56c6c !important;
}

.login-btn {
  float: right;
}
</style>
